import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { z } from '#/adapter/form';
import { getDictOptions } from '#/utils/dict';
import { renderDictTags } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'productName',
    label: '产品名称',
  },
  {
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    fieldName: 'listingTime',
    label: '上架时间',
  },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60, fixed: 'left' },
  {
    title: '产品名称',
    field: 'productName',
    fixed: 'left',
  },
  {
    title: '上架时间',
    field: 'listingTime',
  },
  {
    title: '产品规格',
    field: 'specifications',
  },
  {
    title: '产品类型',
    field: 'productType', // 此处仍然显示 productType，但其值将是 CP,ZP 的组合
    slots: {
      default: ({ row }) => {
        // 根据 isCommercialProduct 和 isGiftProduct 组合显示产品类型
        const types = [];
        if (row.isCommercialProduct) {
          types.push('CP'); // 添加字典code
        }
        if (row.isGiftProduct) {
          types.push('ZP'); // 添加字典code
        }
        // 使用 renderDictTags 方法渲染多个字典标签
        return types.length > 0
          ? renderDictTags(
              types,
              getDictOptions(DictEnum.HM_PRODUCT_TYPE),
              false,
              1,
              'center',
            )
          : '-';
      },
    },
  },
  {
    title: '服用量',
    field: 'remark',
  },
  {
    title: '购买政策',
    field: 'purchasePolicy',
  },
  {
    title: '是否上架',
    field: 'status',
    slots: { default: 'status' },
  },

  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '产品id',
    fieldName: 'productId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '产品名称',
    fieldName: 'productName',
    component: 'Input',
    rules: 'required',
  },
  {
    label: '产品规格',
    fieldName: 'specifications',
    component: 'Input',
    dependencies: {
      rules: (_) => {
        // 修改后的正则表达式支持带单位的数字（如 560g/桶）
        return z
          .string()
          .regex(/^(\d+[\u4E00-\u9FA5A-Z]*)\/[\u4E00-\u9FA5]+$/i, {
            message:
              '格式应为 数字或数字加单位/汉字 组合（例如：560g/桶、10/瓶、10袋/箱）',
          });
      },
      triggerFields: ['specifications'],
    },
  },
  {
    label: '产品类型',
    fieldName: 'productTypesSelected', // 新增一个字段用于 CheckboxGroup 的绑定
    component: 'CheckboxGroup', // 修改为 CheckboxGroup
    rules: 'required', // 至少选择一个
    componentProps: {
      options: getDictOptions(DictEnum.HM_PRODUCT_TYPE), // 字典选项不变
    },
  },
  {
    label: '上架时间',
    fieldName: 'listingTime',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    rules: 'required',
  },
  {
    label: '购买政策',
    fieldName: 'purchasePolicy',
    component: 'Textarea',
    rules: 'required',
  },
  {
    label: '售卖店铺',
    fieldName: 'shopSale',
    component: 'Select',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '服用量',
    fieldName: 'remark',
    component: 'Input',
    dependencies: {
      rules: (model: any) => {
        // 当产品类型为 CP 时启用必填规则
        return model?.productType === 'CP' ? 'required' : '';
      },
      triggerFields: ['productType'],
    },
  },
];
